package com.shiku.im.security.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.shiku.common.model.PageResult;
import com.shiku.im.security.dao.SecurityRoleDao;
import com.shiku.im.security.entity.SecurityRole;
import com.shiku.im.security.entity.SecurityRole2;
import com.shiku.im.security.service.SecurityRoleManager;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author xie yuan yang
 * @Date 2020/3/6
 **/
@Service
public class SecurityRoleManagerImpl implements SecurityRoleManager {

    @Autowired
    private SecurityRoleDao securityRoleDao;

    @Override
    public SecurityRole sava(SecurityRole securityRole) {
        return securityRoleDao.sava(securityRole);
    }

    @Override
    public void deleteRole(ObjectId id) {
        securityRoleDao.deleteRole(id);
    }

    @Override
    public void updateRole(SecurityRole securityRole) {
        securityRoleDao.updateRole(securityRole);
    }

    @Override
    public void updateRoleResource(ObjectId roleId, List<String> roleResourceList) {
        securityRoleDao.updateRoleResource(roleId,roleResourceList);
    }

    @Override
    public PageResult<SecurityRole> querySecurityRole(int pageIndex, int pageSize) {
        return securityRoleDao.querySecurityRole(pageIndex,pageSize);
    }

    @Override
    public SecurityRole querySecurityRoleById(ObjectId id) {
        return securityRoleDao.querySecurityRoleById(id);
    }

    @Override
    public List<SecurityRole> querySecurityRol1e() {
        return securityRoleDao.querySecurityRol1e();
    }

    @Override
    public SecurityRole querySecurityRoleByRoleName(String name) {
        return securityRoleDao.querySecurityRoleByRoleName(name);
    }

    @Override
    public List<SecurityRole> securityRoleManager(String defaultString) {
        List<SecurityRole2> securityRole2s = JSONObject.parseArray(defaultString, SecurityRole2.class);

        List<SecurityRole> securityRoles = new ArrayList<>();
        for (SecurityRole2 securityRole2 : securityRole2s) {
            SecurityRole securityRole = new SecurityRole();
            securityRole.setRoleId(new ObjectId(securityRole2.getRoleId()));
            securityRole.setRoleName(securityRole2.getRoleName());
            securityRole.setRoleDesc(securityRole2.getRoleDesc());
            securityRole.setRoleResourceList(securityRole2.getRoleResourceList());
            securityRole.setStatus(securityRole2.getStatus());
            securityRoles.add(securityRole);
        }
        return securityRoles;
    }

    @Override
    public SecurityRole querySecurityRoleNameByType(byte roleType) {
        SecurityRole securityRole = securityRoleDao.querySecurityRoleNameByType(roleType);
        return securityRole;
    }
}
